package com.webull.information.center.carwler.task;

import java.util.List;
import java.util.function.Consumer;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import com.webull.information.center.carwler.common.model.StockTitle;
import com.webull.information.center.carwler.common.util.NewsTaskUtil;
import com.webull.information.center.carwler.task.handler.StockReuteGuideHandler;
import com.webull.securities.client.model.Fund;
import com.webull.securities.client.model.Stock2Information;
import com.webull.securities.client.model.parm.TickerProfileParm;
import com.webull.securities.client.service.FundService;
import com.webull.securities.client.service.StockService;

@Component
public class News_ReutersGuide_Task {
    protected final Logger logger = LogManager.getLogger(getClass());

    private @Autowired
    StockService stockService;
    private @Autowired
    FundService fundService;
    private @Autowired
    StockReuteGuideHandler stockReuteGuideHandler;
    private @Autowired
    @Qualifier("executor_news")
    ThreadPoolTaskExecutor threadPoolTaskExecutor;
    private final Integer pageSize = 100;

    /**
     * 路透
     *
     * @param regionId
     * @param lastId
     * @param loop
     * @return
     * @since 0.1.0
     */
    public Integer fullReutersGuideStockNews(Integer regionId, Integer lastId, Integer loop) {

        logger.info(
                "News_ReutersEnStok_Task.News_ReutersEnStok_TaskExchangeCode start exchangeCode:{},lastId:{},loop:{}",
                regionId, lastId, loop);
        int loop_ = 0;
        full:
        do {
            if (loop != null && loop > 0 && ++loop_ > loop) {
                break full;
            }
            TickerProfileParm parm = new TickerProfileParm();
            parm.setLastId(lastId);
            parm.setRegionId(regionId);
            parm.setQueryNum(pageSize);
            List<Stock2Information> res = stockService.queryPagingInform(parm);
            if (res == null || res.isEmpty()) {
                lastId = null;
                break full;
            }
            logger.info("News_ReutersEnStok_Task.News_ReutersEnStok_TaskExchangeCode stocks:" + res.size());
            lastId = res.get(res.size() - 1).getTickerId();

            NewsTaskUtil.multiThreadProcess(res, Stock2InfoConSumer, threadPoolTaskExecutor);
        } while (true);
        return lastId;
    }

    private final Consumer<Stock2Information> Stock2InfoConSumer = stock2Icnformation -> {
        StockTitle stockTitle = new StockTitle();
        BeanUtils.copyProperties(stock2Icnformation, stockTitle);
        stockReuteGuideHandler.handlerByStockTitle4Reuters(stockTitle);
        logger.info("News_ReutersEnStok_Task.procss stockTitle:" + stockTitle);
    };


    /**
     * 路透
     *
     * @param regionId
     * @param lastId
     * @param loop
     * @return
     * @since 0.1.0
     */
    public Integer fullReutersGuideFundNews(Integer regionId, Integer lastId, Integer loop) {

        logger.info(
                "News_ReutersEnFund_Task.News_ReutersEnStok_TaskExchangeCode start exchangeCode:{},lastId:{},loop:{}",
                regionId, lastId, loop);
        int loop_ = 0;
        full:
        do {
            if (loop != null && loop > 0 && ++loop_ > loop) {
                break full;
            }

            List<Fund> res = fundService.queryPagingByRegionId(lastId, regionId, pageSize);
            if (res == null || res.isEmpty()) {
                lastId = null;
                break full;
            }
            logger.info("News_ReutersEnFund_Task.News_ReutersEnStok_TaskExchangeCode stocks:" + res.size());
            lastId = res.get(res.size() - 1).getTickerId();

            NewsTaskUtil.multiThreadProcess(res, fundConSumer, threadPoolTaskExecutor);
        } while (true);
        return lastId;
    }

    private final Consumer<Fund> fundConSumer = fund0 -> {
        stockReuteGuideHandler.handlerByFund4Reuters(fund0);
        logger.info("News_ReutersEnStok_Task.procss fund:" + fund0);
    };


}
